package com.dragon.loan.service;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.dragon.loan.entity.SmsSendCount;
import com.dragon.loan.repository.SmsSendCountMapper;
import com.fintech.common.entity.ResultData;

@Service
public class SmsSendCountServiceImpl implements SmsSendCountDragonService {
	
	@Value("${mobile.day.limit}")
	private int mobileDayLimit;
	
	@Value("${ip.day.limit}")
	private int ipDayLimit;
	
	@Value("${mobile.month.limit}")
	private int mobileMonthLimit ;

	@Autowired
	private SmsSendCountMapper smsSendCountMapper;
	
	@Override
	public ResultData<Boolean> isSend(String mobile, String ipAddress) {
		ResultData<Boolean> resultData = new ResultData<>();
		// 首先判断是否允许  判断手机是否超过日限额 跟 是否超过月限额
		Integer selectMobileDayCountByMobile = smsSendCountMapper.selectMobileDayCountByMobile(mobile);
		if(selectMobileDayCountByMobile != null && selectMobileDayCountByMobile >= mobileDayLimit){
			resultData.setErrorMessage("您已超过每日发送限制，请下一日再进行操作");
			resultData.setData(false);
			return resultData;
		}
		
		Integer selectMobileMonthCountByMobile = smsSendCountMapper.selectMobileMonthCountByMobile(mobile);
		if(selectMobileMonthCountByMobile != null && selectMobileMonthCountByMobile >= mobileMonthLimit){
			resultData.setErrorMessage("您已超过每月发送限制");
			resultData.setData(false);
			return resultData;
		}
		// 判断ip地址是否超过月限额
		Integer selectIPDayCountByIP = smsSendCountMapper.selectIPDayCountByIP(ipAddress);
		if(selectIPDayCountByIP != null && selectIPDayCountByIP >= ipDayLimit){
			resultData.setErrorMessage("该IP地址受限，请使用其他网络环境进行操作");
			resultData.setData(false);
			return resultData;
		}
		resultData.setData(true);
		return resultData;
	}

	@Override
	public void countIPAddressAndMobile(String mobile, String ipAddress) {
		// 更新今日的手机日统计
		int updateIncreDayMobile = smsSendCountMapper.updateIncreDayMobile(mobile);
		if(updateIncreDayMobile <= 0){
			insert(mobile, null, "0");
		}
		
		// 更新今日的手机月统计
		int updateIncreMonthMobile = smsSendCountMapper.updateIncreMonthMobile(mobile);
		if(updateIncreMonthMobile <= 0){
			insert(mobile, null, "2");
		}
		
		// 更新今日的IP日统计
		int updateIncreDayIP = smsSendCountMapper.updateIncreDayIP(ipAddress);
		if(updateIncreDayIP <= 0){
			insert(null, ipAddress, "1");
		}
	}
	
	public void insert(String mobile, String ipAddress, String type){
		SmsSendCount smsSendCount = new SmsSendCount();
		smsSendCount.setMobile(mobile);
		smsSendCount.setIpAddress(ipAddress);
		smsSendCount.setCountType(type);
		smsSendCount.setDayDate(new Date());
		smsSendCount.setMonthDate(new Date());
		smsSendCount.setIpDay(1);
		smsSendCount.setMobileDay(1);
		smsSendCount.setMobileMonth(1);
		smsSendCountMapper.insertSelective(smsSendCount);
	}

	@Override
	public ResultData<Boolean> isSend(String mobile, String ipAddress, int mobileDayLimit, int ipDayLimit,
			int mobileMonthLimit) {
		ResultData<Boolean> resultData = new ResultData<>();
		// 首先判断是否允许  判断手机是否超过日限额 跟 是否超过月限额
		Integer selectMobileDayCountByMobile = smsSendCountMapper.selectMobileDayCountByMobile(mobile);
		if(selectMobileDayCountByMobile != null && selectMobileDayCountByMobile >= mobileDayLimit){
			resultData.setErrorMessage("您已超过每日发送限制，请下一日再进行操作");
			resultData.setData(false);
			return resultData;
		}
		
		Integer selectMobileMonthCountByMobile = smsSendCountMapper.selectMobileMonthCountByMobile(mobile);
		if(selectMobileMonthCountByMobile != null && selectMobileMonthCountByMobile >= mobileMonthLimit){
			resultData.setErrorMessage("您已超过每月发送限制");
			resultData.setData(false);
			return resultData;
		}
		// 判断ip地址是否超过月限额
		Integer selectIPDayCountByIP = smsSendCountMapper.selectIPDayCountByIP(ipAddress);
		if(selectIPDayCountByIP != null && selectIPDayCountByIP >= ipDayLimit){
			resultData.setErrorMessage("该IP地址受限，请使用其他网络环境进行操作");
			resultData.setData(false);
			return resultData;
		}
		resultData.setData(true);
		return resultData;
	}

}
